package com.expense.app.admin.services.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.common.BaseService;
import com.expense.app.admin.model.dto.job.JobImportDTO;
import com.expense.app.admin.model.dto.school.*;
import com.expense.app.admin.model.params.job.JobImportParam;
import com.expense.app.admin.model.params.school.*;
import com.expense.app.admin.services.SchoolServ;
import com.expense.common.constant.enums.DeletedStatus;
import com.expense.common.dao.SchoolDao;
import com.expense.common.exception.PlatformException;
import com.expense.common.model.School;
import com.expense.common.model.auth.AdminUser;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 全国院校名单
 */
@Service
public class SchoolServImpl extends BaseService implements SchoolServ {
    private static final Log log = LogFactory.get();

    @Autowired
    SchoolDao schoolDao;


    @Override
    public JobImportDTO importExcel(JobImportParam param) {
        AdminUser loginUser = getCurrentUser();
        List<Map<String,String>> list = param.getMapList();
        List<Map<String,String>> errorList = new ArrayList<>();
        List<Map<String,String>> errorDataList = new ArrayList<>();
        if(list != null && list.size()>0) {
            List<School> jobList = new ArrayList<>();
            for (Map<String, String> map : list) {
                String sort = map.get("序号");
                String name = map.get("学校名称");
                String code = map.get("学校标识码");
                String dept = map.get("主管部门");
                String city = map.get("所在地");
                String rank = map.get("办学层次");
                String remark = map.get("备注");

                if (StrUtil.isBlank(name)) {
                    continue;
                }

                School entity = new School();
                entity.setSort(Convert.toInt(sort,99999));
                entity.setName(name);
                entity.setCode(code);
                entity.setDept(dept);
                entity.setCity(city);
                entity.setRank(rank);
                entity.setRemark(remark);

                setBaseInfo(entity);

                jobList.add(entity);

            }
            schoolDao.saveAll(jobList);
        }
        JobImportDTO dto = new JobImportDTO();
        dto.setTotalCount(list.size());
        dto.setSuccessCount(list.size() - errorList.size());
        dto.setErrorCount(errorList.size());
        dto.setErrorList(errorList);
        dto.setErrorDataList(errorDataList);
        return dto;
    }

    @Override
    public SchoolListDTO findList(SchoolListParam param) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("key",param.getKey());
        PageQuery pageQuery = initPageQuery(param.getPage(),param.getSort());
        pageQuery.setParas(condition);
        schoolDao.findListPage(pageQuery);
        return new SchoolListDTO(pageQuery.getList(),pageQuery.getTotalRow());
    }

    @Override
    public School findOne(Long id) {
        return schoolDao.findByIdAndDeleted(id);
    }

    @Override
    public SchoolDTO findDetail(Long id) {
        School entity = schoolDao.findByIdAndDeleted(id);
        return new SchoolDTO(entity);
    }

    @Override
    public School saveOne(SchoolParam param) {
        School entity = param.getEntity();
        setBaseInfo(entity);
        schoolDao.saveOne(entity);
        //其他关联处理
        return entity;
    }

    @Override
    public Long deleteOne(Long id) {
        School entity = schoolDao.findByIdAndDeleted(id);
        if(entity == null) throw new PlatformException("全国院校名单已不存在");
        entity.setDeleted(DeletedStatus.DELETED);
        setBaseInfo(entity);
        schoolDao.updateTemplateById(entity);
        //其他关联处理
        return id;
    }
}
